package top.houry.im.client.handler.impl;

import io.netty.channel.ChannelHandlerContext;
import javafx.application.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.houry.im.client.controller.ImClientLoginUiController;
import top.houry.im.client.controller.ImClientMainUiController;
import top.houry.im.client.event.ImClientUiLoginEvent;
import top.houry.im.client.handler.ImClientMsgTypeHandler;
import top.houry.im.client.proto.ImMessage;
import top.houry.im.ui.controller.ImUiMainController;

import java.util.Date;

public class ImClientMsgRecvServiceImpl implements ImClientMsgTypeHandler {
    private static final Logger log = LoggerFactory.getLogger(ImClientMsgRecvServiceImpl.class);
    @Override
    public void processImMsg(ImMessage.Client client, ChannelHandlerContext ctx) {
        try {
            ImMessage.ChatRespMessage chatRespMessage = ImMessage.ChatRespMessage.parseFrom(client.getBytesData());
            String chatMsg = chatRespMessage.getChatMsg();
            String msgDate = chatRespMessage.getMsgDate();
            String fromUserId = chatRespMessage.getFromUserId();
            String toUserId = chatRespMessage.getToUserId();
            String userName = chatRespMessage.getUserName();

            ImUiMainController instance = ImClientMainUiController.getInstance();

            Platform.runLater(() -> {
                instance.addTalkBox(-1, 0, String.valueOf(fromUserId), userName, "", chatMsg,  new Date(), false);
                instance.addTalkMsgUserLeft(fromUserId, chatMsg, 0, new Date(), false, false, false);
            });

        } catch (Exception e) {

        }
    }
}
